package me.sdau.spider.manager.modules.system.controller;

import me.sdau.spider.manager.common.entity.bo.Tree;
import me.sdau.spider.manager.common.utils.SystemUtil;
import me.sdau.spider.manager.modules.system.entity.Menu;
import me.sdau.spider.manager.modules.system.entity.User;
import me.sdau.spider.manager.modules.system.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @author zj
 * @date 2018/4/9 19:15
 */
@Controller
public class LoginController {

    @Autowired
    private MenuService menuService;

    @GetMapping({"", "/index"})
    public String index(Model model) {
        User user = SystemUtil.getUser();
        List<Tree<Menu>> trees = menuService.listMuneTree(user.getId());
        model.addAttribute("user", user);
        model.addAttribute("munes", trees);
        return "index";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public void ajaxLogin(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
    }
}
